//
//  MessageViewController.m
//  e律师
//
//  Created by 韦淋 on 2017/5/15.
//  Copyright © 2017年 test. All rights reserved.
//

#import "MessageViewController.h"



@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource,LynnTypeButtonActionDelegate>

{
    NSString *cellIdentifier;
    NSInteger flag;
}

@property (nonatomic, strong) UITableView *messageTableView;
@property (nonatomic, strong) NSMutableArray *messageDataSource;

@end


@implementation MessageViewController
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    //    self.automaticallyAdjustsScrollViewInsets = false;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.barTitleLable.text = @"消息通知";
    self.view.backgroundColor = [UIColor whiteColor];
    [self InitializeInterface];
    flag = 0;
    self.messageDataSource = [NSMutableArray array];
    [self getMessage];
    
}
- (void)InitializeInterface{
    
    LynnTypeButtonView  *typeBtnView = [[LynnTypeButtonView  alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 44)];
    [typeBtnView setTypeButtonTitles:@[@"消息通知",@"开庭消息"] withDownLableHeight:2 andDeleagte:self];
    [typeBtnView setTypeButtonNormalColor:[UIColor blackColor] andSelectColor:[UIColor colorWithRed:63.0f/255.0f green:189.0f/255.0f blue:240.0f/255.0f alpha:1.0f]];
    typeBtnView.backgroundColor = [UIColor clearColor];
    
    
    [self.view insertSubview:typeBtnView atIndex:0];
    [self.view insertSubview:self.messageTableView belowSubview:typeBtnView];
    
}
#pragma mark - 网络请求
- (void)getMessage{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    if (flag == 0) {
        
        [NetworkingManager postUrl:[NSString stringWithFormat:@"%@accepterId=%@",MESSAGE_URL,[InfoSingleTon personInfoSingleTon].infoDic[@"user"][@"id"]] dic:nil Succeed:^(id responseData) {
            self.messageDataSource = responseData[@"list"];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            NSLog(@"messageData = %@",self.messageDataSource);
            [self.messageTableView reloadData];
        } failure:^(NSError *error) {
            [MBProgressHUD showError:@"网络异常"];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }
    else if (flag == 1){
        [NetworkingManager postUrl:[NSString stringWithFormat:@"%@create_id=%@",COURTINFO_URL,[InfoSingleTon personInfoSingleTon].infoDic[@"user"][@"id"]] dic:nil Succeed:^(id responseData) {
            self.messageDataSource = responseData[@"list"];
            NSLog(@"messageData = %@",responseData);
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [self.messageTableView reloadData];
        } failure:^(NSError *error) {
            [MBProgressHUD showError:@"网络异常"];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }
    
    
}
#pragma mark - TypeButtonDelegate
//实现代理
-(void)didClickTypeButtonAction:(UIButton*)button withIndex:(NSInteger)index
{
    flag = index;
    self.messageDataSource = nil ;
    [self.messageTableView reloadData];
    [self getMessage];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.messageDataSource.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (flag == 0) {
        MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"messageCell" forIndexPath:indexPath];
        cell.titleLable.text = self.messageDataSource[indexPath.row][@"theme"];
        cell.contentLable.text = self.messageDataSource[indexPath.row][@"content"];
        NSTimeInterval _interval=[self.messageDataSource[indexPath.row][@"createDate"] doubleValue] / 1000.0;
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        NSString *string = [formatter stringFromDate:date];
        
        cell.dateLable.text = string;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        cell.layer.shadowOffset =CGSizeMake(0, 1);
//
//        cell.layer.shadowColor = [UIColor lightGrayColor].CGColor;
//        cell.layer.shadowRadius = 1;
//        cell.layer.shadowOpacity = .5f;
//        
//        
//        CGRect shadowFrame = cell.layer.bounds;
//        CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath;    cell.layer.shadowPath = shadowPath;
        return cell;
        
    }
    else{
        NoticeOfTrialCell *cell = [tableView dequeueReusableCellWithIdentifier:@"noticeOfTrialCell" forIndexPath:indexPath];
        return cell;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetworkingManager getUrl:[NSString stringWithFormat:@"%@id=%@",MYCASES_URL,self.messageDataSource[indexPath.row][@"glid"]] dic:nil Succeed:^(id responseData) {
        NSLog(@"responseData = %@",responseData);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CaseDetailViewController *caseDetailVC = [[CaseDetailViewController alloc] init];
        caseDetailVC.caseDetailDic = responseData[@"result"][0];
        caseDetailVC.caseDetailFlag = 8;
        [self.navigationController pushViewController:caseDetailVC animated:YES];
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络异常"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
    
}



#pragma mark - Getter/Setter


- (UITableView *)messageTableView{
    if (!_messageTableView) {
        _messageTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 110, SCREEN_WIDTH, SCREEN_HEIGHT - 110) style:UITableViewStylePlain];
        _messageTableView.backgroundColor = [UIColor clearColor];
        _messageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _messageTableView.delegate = self;
        _messageTableView.dataSource = self;
        _messageTableView.estimatedRowHeight = 180;
        [_messageTableView registerClass:[MessageCell class] forCellReuseIdentifier:@"messageCell"];
        [_messageTableView registerClass:[NoticeOfTrialCell class] forCellReuseIdentifier:@"noticeOfTrialCell"];
    }
    return _messageTableView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
